package cn.edu.zafu.bigdata.x.wupeng.j;

import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TcpClient1 {
    public static void main(String[] args) throws IOException {
        Socket sock = new Socket("localhost", 6666);// 连接指定服务器和端口
        try(InputStream input = sock.getInputStream()){
            try(OutputStream output = sock.getOutputStream()){
                handle(input,output);
            }
        }
        sock.close();
        System.out.println("disconnected.");
    }

    private static void handle(InputStream input,OutputStream output) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));
        Scanner scanner = new Scanner(System.in);
        System.out.println("[来自服务器的问候]"+reader.readLine());
        while (true){
            System.out.print("请输入：");
            String line = scanner.nextLine();
            writer.write(line);
            writer.newLine();
            writer.flush();
            System.out.println("[来自服务器的回复]"+reader.readLine());
            if ("bye".equalsIgnoreCase(line)){
                break;
            }
        }
    }
}

